﻿using System;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace Zapi.Api.Filters
{
    /// <summary>
    /// 实体验证
    /// </summary>
    public class ModelValidFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (!actionContext.ModelState.IsValid)
            {
                if (!actionContext.ModelState.IsValid)
                {
                    foreach (var value in actionContext.ModelState.Values)
                    {
                        if (value.Errors.Count > 0)
                        {
                            foreach (var error in value.Errors)
                            {
                                throw new Exception(error.ErrorMessage);
                            }
                        }
                    } 
                }

            }

        }
    }


}